package com.zs.service;

import com.zs.spring.BeanPostProcessor;
import com.zs.spring.Component;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Author: ZhengShan
 * @CreateTime: 2024-06-30
 * @Description:
 * @Version 1.0
 */
@Component
public class ZhengshanBeanPostProcess implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(String beanName, Object o) {
        if("orderService".equals(beanName)){
            //针对对象额外处理
            System.out.println("------针对对象初始化前额外处理");
        }
        return o;
    }

    @Override
    public Object postProcessAfterInitialization(String beanName, Object o) {
        if(beanName.equals("testService")) {
            Object proxyInstance = Proxy.newProxyInstance(ZhengshanBeanPostProcess.class.getClassLoader(), o.getClass().getInterfaces(), new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    System.out.println("切面逻辑");
                    return method.invoke(o, args);
                }
            });
            return proxyInstance;
        }
        return o;
    }
}
